import * as ENV from '../configs/env'

const wxpay = (opts) => {
    var homeUrl = location.origin
    var redirectUrl = homeUrl
    var paramSplit = '&'
    var payAction = ENV.MODE == 'PE' ? 'http://pay.ddxq.mobi/pay/wx.php' : 'http://paytest.ddxq.mobi/pay/wx.php'

    var form = document.createElement("form")
    form.action = payAction
    form.method = 'POST'

    function createInput(key, val) {
        var input = document.createElement("input")
        input.type = "hidden"
        input.name = key
        input.value = val
        form.appendChild(input)
    }

    createInput('site', 'freelancer')
    createInput('order_id', opts.orderId)
    createInput('pay_url', opts.url)
    createInput('redirect_url', opts.redirectUrl || redirectUrl)
    createInput('home_url', homeUrl)
    createInput('param_split', paramSplit)

    document.body.appendChild(form)
    try {
        form.submit()
    } catch (ex) {
        alert("出现异常");
    }
}

export default {
    wxpay
}